

function QTColumn(column){
	this.title = null;
	this.render = null;
	this.align = null;
	this.width = null;
	this.type = null;
	this.dict = null;			
	this.order = null;			//是否需要排序，false不排序
	this.defOrder = null;		//默认排序列
	
	this.copy(column)
}

QTColumn.prototype.copy = function(c){
	if (!c) return;
	if (typeof c == "object"){
		for (prop in this){
			if(this.hasOwnProperty(prop)){
				if ((typeof this[prop] != "number")&&(typeof this[prop] != "boolean")&&!this[prop]) 
					this[prop] = c[prop]
			}
		}
	}
}
function PageObject(data){
	this.start = 0;
	this.maxResult = getCookie("pageLen")?getCookie("pageLen"):Common.getMaxResult();
	this.total = 0;
	this.objects = [];
	
	if (data instanceof Array){
		this.maxResult = data.length;
		this.total = data.length;
		this.objects = data;
	}else if (data&&typeof data == "object"){
		if (data.objects instanceof Array)
			this.objects = data.objects;
		if (typeof data.start == "number")
			this.start = data.start;
		if (typeof data.maxResult == "number")
			this.maxResult = data.maxResult;
		if (typeof data.start == "number")
			this.total = data.total;	
	}
}


function QueryTable(queryName,searchFormId,tableId,columns,options){
	/**
	* 依赖的外界方法 $、isIE、encodeURIComponent,checkForm,Array.prototype.add & remove,
	*/
	if(!options) 
		options = {}
	

	this.inited = false;
	this.queryName = queryName;
	this.tableId = tableId;
	this.searchFormId = searchFormId;			
	this.dataSrc = null;
	this.title = options.title?options.title:"";
	this.service = options.service;
	
	this.start=0;
	this.maxResult = getCookie("pageLen")?getCookie("pageLen"):10;	
	this.total=0;
	
	this.pageMultiple = 10;
	this.minRows = 10;

	this.params = null;
	this.paramFunc = options.paramFunc;
	
	this.columns = [];							//定制的列信息,默认是全部列
	
	
	var me = this;
	
	var columnsConf = options.columnsConf;
	var forceFit = options.forceFit;
	var showData = options.showData;
	var isPage = options.isPage == false?false : true;							//是否显示翻页
	var isFull = options.isFull == false?false : true;							//是否用空行补齐数据行

	var colMap = {}								//列map
	var itemMap = {}							//存储tr、td map
	
	var tId = 1;
	
	var initcols = false;						//初始化列是否完成
	var loading = false;						//载入数据标志
	var orderFlag = false;						//是否可以排序
	
	var checkRadio;								//被选中的radio
	var defWidth;								//默认宽度
	var orderColumn;
	
	var tableLayer = null;
	var exportLayer = null;
	var confLayer = null;						//定制显示列的层对象
	var dragLayer = null;						//托拽列时候的层对象
	var flagLayer = null;						//标志托拽列放置位置的层对象
	var separLayer = null;						//调整列宽度的分隔线层对象
	
	var htId = me.tableId +"_table_head";				//head table id
	var dtId = me.tableId +"_table_data";				//data table id
	var tableWidth = 0;									//table 宽度
	var flagsPos = [];									//标志坐标
		
	var bTrId = function(floor){				//生成tr的id
		return me.tableId +"_"+floor+"_tr";
	}
	var bTdId = function(floor,col){			//生成td的id
		return me.tableId +"_"+floor+"_td_"+col;	
	}
	var bCbId = function(floor){				//生成td的id
		return me.tableId +"_"+floor+"_checkbox";	
	}
	var bOdId = function(col){					//生成排序div的id
		return me.tableId +"_orderby_"+col;		
	}
	
	//获得查询条件方法
	var getParams = function(){
		if (me.paramFunc)
			return me.paramFunc()
		else if (me.searchFormId)
			return Common.getValues(me.searchFormId)
		else
			return {}
	}
	//获取源数据
	var gds = function(){
		if (!(me.dataSrc instanceof PageObject)){
			me.dataSrc = new PageObject(me.dataSrc);
		}
		return me.dataSrc;
	}
	//初始化列宽度
	var buildColumnsWidth1 = function(nw,total,cbc){
		tableWidth = $(me.tableId+'_datas').offsetWidth
		defWidth = Math.floor((tableWidth - cbc * QueryTable.CR_SIZE) / me.columns.length)
		var bw = tableWidth - defWidth * (nw - cbc) - cbc * QueryTable.CR_SIZE; 
		var j = 0;
		var cwt = 0;
		for (var i=0;i<me.columns.length;i++){
			var column = me.columns[i];
			if (column.type == "checkbox" || column.type == "radio"){
				column.width = QueryTable.CR_SIZE;
			}else{
				var w = column.width
				if (!w){
					column.width = defWidth;
				}else{
					column.width = Math.round(bw * w/total);
				}
				column.original = column.width;
				if (column.checked)
					cwt += column.original;
			}
		}
		
		bw = tableWidth - cbc * QueryTable.CR_SIZE;
		var j = 0;
		for (var i=0;i<me.columns.length;i++){
			var column = me.columns[i];
			if (column.checked){
				if (column.type == "checkbox" || column.type == "radio"){
					
				}else{
					column.width = Math.round(bw * column.original/cwt);
					j += column.width;
				}
			}
		}
		for (var i=me.columns.length-1;i>-1;i--){
			var column = me.columns[i];
			if (column.checked){
				column.width += bw - j;
				break;
			}
		}
	}
	//初始化显示区域，title，button，数据区域
	var initView = function(){	
		var div = $(me.tableId)
		separLayer = $(me.tableId+'_separ');
		$(me.tableId+'_title').innerHTML = me.title?me.title:'';
		createHeadTable(me.tableId+"_heads");
		
		var div = $(me.tableId+'_datas')
		div.style.height = options.height?options.height:"250px";
		div.style.width = (tableWidth + 17) + "px";
		if (!showData){
			createDataTable();
			getTableLayer();
		}else{
			getTableLayer().style.display = "none";
			me.showTable();
		}
		if (options.initFunc)
			options.initFunc();
		me.inited = true;
	}
	
	/** 创建按钮
	 * @param id 按钮的id
	 * @param value 按钮内容
	 * @param actions 点击按钮触发的方法
	 */
	var createButton = function(id,value,actions,type,disabled){
		if (typeof type == "string")
			type = type.toUpperCase()
		else
			type = "BUTTON";
		var btn = document.createElement(type);
		//btn.setAttribute = ("type","button");
		if (id)
			btn.id = id;
		btn.disabled = disabled;
		btn.innerHTML = value;
		if (typeof actions == "function") btn.onclick = actions
		return btn;
	}

	//生成button
	var createButtons = function(divId){
		var div = $(divId);
		if(typeof options.doms == "object"){
			var d = createButton(options.doms.id,options.doms.value,options.doms.actions,options.doms.type);
			div.appendChild(d);
		}
		
		if (options.exportable == true){
			var b = createButton("export","导 出",exportClick);
			bind(b,"mouseout",hideExportLayer);
			div.appendChild(b);
		}
		if (!(options.buttons instanceof Array)) return;
		for (var i = 0; i<options.buttons.length; i++){
			var b = options.buttons[i]
			if (typeof b == "object"){
				b = createButton(b.id?me.tableId + "_btn_" + b.id:'',b.value,b.actions,b.type,b.disabled);
			}
			if (b && b.type == "button")
				div.appendChild(b)
		}
	}
	var exportClick = function(){
		var cl = getExportLayer(this);
		cl.style.display = "";
		cl.innerHTML = "";
		var bts = ["xml","pdf","xls","csv","rtf"]
		for (var i=0;i<bts.length;i++){
			var c = bts[i];
			var div = document.createElement("DIV");
			var a = document.createElement("DIV");
			a.className = QueryTable.EXPORT_DIV_OUT;
			a.align = "center";
			a.innerHTML = c;
			div.appendChild(a);
			cl.appendChild(div);
			bind(a,"mouseout",function(){this.className = QueryTable.EXPORT_DIV_OUT})
			bind(a,"mouseover",function(){this.className = QueryTable.EXPORT_DIV_OVER})
			bind(a,"click",function(){exportList(this.innerHTML)})
		}
	}
	//创建table
	var ct = function(div,id){
		if (typeof div != "object")
			div = $(div)
		var table = document.createElement("TABLE");
		if (id) table.id = id;
		table.width = tableWidth;
		table.border = "0";
		table.cellPadding = 0;
		table.cellSpacing = 0;
		div.appendChild(table);
		var tbody = document.createElement("TBODY");
		table.appendChild(tbody);
		return tbody;
	}
	//生成checkbox、radio
	var ci = function(type,value,floor){
		var input;
		if (isIE()){
			input = document.createElement("<INPUT NAME='"+me.tableId + "_cbrs"+"'>")
		}else{
			input = document.createElement("INPUT")
			input.name = me.tableId + "_cbrs";
		}
		input.type = type
		if (floor == 0 || floor){
			input.id = bCbId(floor);
			input.floor = floor;
		}
			input.value = value;
		return input
	}
	//生成表头
	var createHeadTable = function(divId){
		killChildren(divId);
		var tbody = ct(divId,htId);		
		//生成数据表头,包括定制、托拽列宽度的功能
		var tr = document.createElement("TR");
		tbody.appendChild(tr);
		tr.id = bTrId(-1);
		tr.style.height = "24px";
		for (var i=0; i<me.columns.length; i++){
			var c = me.columns[i];
			if (!c.checked) continue;
			var td = document.createElement("TD");
			tr.appendChild(td)
			td.id = bTdId(-1,i);
			td.style.width = c.width + "px";
			td.floor = -1;
			td.column = i;
			
			if (c.type == "checkbox"){
				var input = ci("checkbox");
				td.appendChild(input) 
				bind(input,"click",cbAll)
			}else if (c.type == "radio"){
				var input = ci("radio");
				td.appendChild(input) 
			}else{
				var div1 = document.createElement("DIV");
				var ptd = td.previousSibling;
				var b = true;
				if (ptd){
					var pc = me.columns[ptd.column];
					if (pc.type == "checkbox" || pc.type == "radio"){
						b = false;
					}
				}else
					b = false;
				td.appendChild(div1);
				if (b){																	//checkbox的列宽度不变
					div1.className = QueryTable.HEADER_LEFT_CLASS;
					div1.column = i-1;
					bind(div1,"mousedown",mouseDown)
					bind(div1,"mouseup",mouseUp)
					bind(div1,"mousemove",mouseMove)
				}else{
					div1.className = QueryTable.NORMAL_LEFT_CLASS;
				}
				var div = document.createElement("DIV");
				td.appendChild(div)
				div.id = bOdId(i);
				if (c.defOrder) {
					div.dasc = c.defOrder === true?"desc":c.defOrder;
					div.className = div.dasc == "desc"?QueryTable.ORDERBY_DESC:QueryTable.ORDERBY_ASC;
					me.orderby = c.col + " " + div.dasc;
					orderColumn = div;
				}
				div.style.width = (c.width - div1.offsetWidth - 4) + "px";
				div.style.height = "24px";
				div.style.overflow = "hidden";
				if (c.align)
					div.style.textAlign = c.align;
				div.innerHTML = c.title;
				div.orderby = i;
				
				bind(div,"mousedown",dragDwon)
				bind(div,"mouseup",dragUp)
				bind(div,"mousemove",dragMove)
				if (c.order != false)
					bind(div,"click",orderFunc)
			}
		}
		if (forceFit == true){
			var td = document.createElement("TD");
			td.width = 3
			tr.appendChild(td)
			var div1 = document.createElement("DIV");
			var ptd = td.previousSibling;
			if (ptd){
				var pc = me.columns[ptd.column];
				if (pc.type == "checkbox" || pc.type == "radio"){
					return;
				}
			}else
				return;
			td.appendChild(div1);
			div1.className = QueryTable.LAST_LEFT_CLASS;
			div1.borderLeft = 0
			div1.column = i-1;
			bind(div1,"mousedown",mouseDown)
			bind(div1,"mouseup",mouseUp)
			bind(div1,"mousemove",mouseMove)
		}
	}
	//生成数据表格
	var createDataTable = function(){
		var divId = me.tableId+"_datas";
		killChildren(divId);
		
		me.start = gds().start;
		me.maxResult = gds().maxResult;
		me.total = gds().total;
		var datas = gds().objects;
		var tbody = ct(divId,dtId);
		for(var i=0; i<datas.length; i++){
			var d = datas[i];
			tbody.appendChild(createTR(d,i));
		}
		/**
		var len = datas.length;
		if (isFull&&len<me.maxResult){
			for (var i=0; i<me.maxResult - len; i++){
				tbody.appendChild(createTR(null,i+datas.length));
			}
		}
		*/
		if (isPage)
			createPaginate(me.tableId+"_page");
	}
	//生成数据表行数据
	var createTR = function(data,floor){
		var tr = document.createElement("TR");
		bind(tr,"mouseover",trOver)
		bind(tr,"mouseout",trOut)
		bind(tr,"click",trClick)
		tr.id = bTrId(floor);
		tr.style.height = "24px";
		if(!isIE()){
			tr.style.height = "25px";
		}
		tr.floor = floor;
		for (var i=0; i<me.columns.length; i++){
			var c = me.columns[i];
			if (!c.checked) continue;
			var td = document.createElement("TD");
			td.style.width = c.width + "px";
			td.style.height = QueryTable.CR_SIZE;
			td.id = bTdId(floor,i);
			td.floor = floor;
			td.column = i;
			if (data){
				var v = data[c.col];
				if (c.type == "checkbox" || c.type == "radio"){
					var input = ci(c.type,typeof c.render == "function"?c.render(v,data,floor):v,floor);
					td.appendChild(input) 
					bind(input,"click",cbOne)
				}else if (c.dict){
					acTdDiv(td,Common.getText(c.dict,v),c);
				}else if (typeof c.render == "function"){
					acTdDiv(td,c.render(v,data,floor),c);
				}else
					acTdDiv(td,v,c);
			}else{
				acTdDiv(td,null,c);
			}
			tr.appendChild(td);
		}
		return tr;
	}
	var acTdDiv = function(td,v,c){
		var div = document.createElement("DIV");
		div.className = QueryTable.DATA_TD_DIV;
		if (c.align)
			div.style.textAlign = c.align;
		div.innerHTML = typeof v == "number" || typeof v == "boolean" || v?v:"&nbsp;";
		td.appendChild(div);
	}
	//显示列放置位置的层对象
	var showFlag = function(x){
		for (var i = 0; i<flagsPos.length; i++){
			var fp = flagsPos[i];
			if (fp.l < x && fp.r >= x){
				var fl = getFlagLayer();
				var t = fl.childNodes[0]
				fl.style.left = fp.pos.left - Math.floor(t.offsetWidth/2);
				fl.style.top = fp.pos.top - t.offsetHeight;
				fl.column = fp.column;
				fl.style.display = "";
			}
		}
	}
	
	var dragMove = function(){
		if (event.button != 1) return;
		if (!this.moveStart) return;
		var id = me.tableId + "_d_l_content";
		var dl = getDragLayer();
		if (!this.move){
			flagsPos = [];
			var tr = $(bTrId(-1));
			var len = forceFit?tr.childNodes.length-1:tr.childNodes.length
			for(var i=0; i<len; i++){
				var td = tr.childNodes[i];
				var pos = absp(td);
				var mpx = pos.left + Math.floor(td.offsetWidth/2);
				flagsPos.push({l:pos.left,r:mpx,column:td.column,pos:pos});
				
				var el = pos.left + td.offsetWidth;
				flagsPos.push({l:mpx,r:el,column:td.column + 1,pos:{left:el,top:pos.top}})
			}
			
			var div = document.createElement("DIV")
			div.id = id;
			div.style.height = this.parentNode.offsetHeight - 2;
			var p = absp(this)
			dl.innerHTML = "";
			dl.style.display = "";
			dl.style.left = p.left + 2;
			dl.style.top = p.top + 2;
			dl.left = p.left + 2;
			dl.top = p.top + 2;
			dl.style.width = (this.parentNode.offsetWidth + 24) + "px";
			dl.style.height = this.parentNode.offsetHeight;
			dl.appendChild(div);
		
			var div1 = document.createElement("DIV")
			div1.className = QueryTable.DARG_LAYER_CONTENT_TITLE;
			div1.innerHTML = this.innerHTML;
			div1.style.width = this.parentNode.offsetWidth + "px";
			div1.style.height = 14 + "px";
			div1.style.overflow = "hidden";
			div.appendChild(div1);
			this.move = true;
		}
		var x = event.clientX - this.startX;
		var y = event.clientY - this.startY;
		var dl = getDragLayer();
		dl.style.left = dl.left + x;
		dl.style.top = dl.top + y;
		showFlag(event.clientX)
		
		var lf = getFlagLayer();		
		var newCol = lf.column;
		var oldCol = this.column;
		if (newCol == oldCol || newCol == oldCol + 1) {
			$(id).className = QueryTable.DARG_LAYER_CONTENT_NO;
		}else{
			$(id).className = QueryTable.DARG_LAYER_CONTENT_YES;
		}
	}
	var dragDwon = function(){
		if (event.button != 1) return;
		if (this.moveStart) return;
		//td 坐标、1坐标
		this.moveStart = true;
		this.move = false;
		this.startX = event.clientX;
		this.startY = event.clientY;
		this.setCapture();
		this.column = this.parentNode.column;
	}
	var dragUp = function(){
		if(!this.moveStart) return;
		if(!this.move) return;
		
		this.moveStart = false;
		this.releaseCapture();
		getDragLayer().style.display = "none";
		var lf = getFlagLayer();
		lf.style.display = "none";
		var newCol = lf.column;
		var oldCol = this.column;
		if (newCol == oldCol || newCol == oldCol + 1) return;
		var c = me.columns[oldCol];
		if (newCol > oldCol){
			me.columns.add(newCol,c);
			me.columns.remove(oldCol);
		}else{
			me.columns.remove(oldCol);
			me.columns.add(newCol,c);
		}
		createHeadTable(me.tableId+"_heads");
		createDataTable()
	}
	
	//排序
	var orderFunc = function(){
		this.moveStart = false;
		this.releaseCapture();
		if (!orderFlag) return;
		if (this.dasc == "asc") {
			this.className = QueryTable.ORDERBY_DESC;
			this.dasc = "desc"
		}else{
			this.className = QueryTable.ORDERBY_ASC;
			this.dasc = "asc"
		}
		if (orderColumn && orderColumn!=this)
			orderColumn.className = "";
		orderColumn = this;
		
		me.orderby = this.orderby + " "+this.dasc;
		me.start = 0;
		me.onclickActions(0);
	}
	//获得表头托拽层对象
	var getDragLayer = function(){
		if (!dragLayer){
			dragLayer = document.createElement("DIV")
			dragLayer.className = QueryTable.DRAG_LAYER_CLASS;
			dragLayer.style.display = "none";
			dragLayer.innerHTML = this.innerHTML;
			$(me.tableId).appendChild(dragLayer);
		}
		return dragLayer;
	}
	//搜索提示
	var getTableLayer = function(){
		if (!tableLayer){
			var div = $(me.tableId+"_datas");
			var position = absp(div);
			tableLayer = document.createElement("DIV");
			tableLayer.className = QueryTable.TABLE_LAYER_CLASS;
			tableLayer.innerHTML = "<table width=100% height=50><tr><td align=center class=gray><!---->提示：输入查询条件后点击[查询]按钮，查询结果会显示在这里</td></tr></table>";
			var ow = tableWidth;
			var oh = div.offsetHeight -1;
			var ol = position.left;
			var ot = position.top;
			if(isIE()){
				ol = ol + 1;
				ot = ot + 1;
			}
			tableLayer.style.width = ow + "px";
			tableLayer.style.height = oh + "px";
			tableLayer.style.left = ol + "px";
			tableLayer.style.top = ot + "px";
			$(me.tableId).appendChild(tableLayer);
		}
		return tableLayer;
	}
	//获得标记层对象
	var getFlagLayer = function(){
		if (!flagLayer){
			flagLayer = document.createElement("DIV")
			flagLayer.className = QueryTable.FLAG_LAYER_CLASS;
			flagLayer.style.display = "none";
			var div1 = document.createElement("DIV")
			div1.className = QueryTable.COL_MOVE_TOP;
			var div2 = document.createElement("DIV")
			div2.className = QueryTable.COL_MOVE_MIDDLE;
			div2.style.height = $(htId).offsetHeight;
			var div3 = document.createElement("DIV")
			div3.className = QueryTable.COL_MOVE_BOTTOM;
			flagLayer.appendChild(div1);
			flagLayer.appendChild(div2);
			flagLayer.appendChild(div3);
			$(me.tableId).appendChild(flagLayer);
		}
		return flagLayer;
	}
	//获得导出层对象
	var getExportLayer = function(ex){
		if (!exportLayer){
			var positon = absp(ex);
			exportLayer = document.createElement("DIV");
			exportLayer.id = me.tableId + "_export_layer";
			exportLayer.className = QueryTable.CONF_LAYER_CLASS;
			exportLayer.style.left = positon.left + "px";
			exportLayer.style.top = (positon.top + ex.offsetHeight - 4) + "px";
			exportLayer.style.width = (ex.offsetWidth - 8) + "px";
			$(me.tableId).appendChild(exportLayer);
			bind(exportLayer,"mouseout",hideExportLayer)
			bind(exportLayer,"mouseover",showExportLayer)
		}
		return exportLayer;
	}
	//获得定制列层对象
	var getConfLayer = function(conf){
		if (!confLayer){
			var positon = absp(conf);
			confLayer = document.createElement("DIV");
			confLayer.id = me.tableId + "_conf_layer";
			confLayer.className = QueryTable.CONF_LAYER_CLASS;
			confLayer.style.left = positon.left + "px";
			confLayer.style.top = (positon.top + conf.offsetHeight) + "px";
			$(me.tableId).appendChild(confLayer);
			bind(confLayer,"mouseout",hidConfLayer)
			bind(confLayer,"mouseover",showConfLayer)
		}
		return confLayer;
	}
	//定制方法
	var confFunc = function(){
		var cl = getConfLayer(this);
		cl.style.display = "";
		cl.innerHTML = "";
		
		for (var i=0;i<me.columns.length;i++){
			var c = me.columns[i];
			
			if (!c.title) continue;				//TODO 用hidden判断最好
			var div = document.createElement("DIV");
			var span = document.createElement("SPAN");
			span.innerHTML = c.title;
			var cb;
			if (isIE()&&c.checked){
				cb = document.createElement("<INPUT CHECKED>")
			}else{
				cb = document.createElement("INPUT")
				cb.checked = c.checked;
			}
			cb.type = "checkbox";
			cb.value = i;
			div.appendChild(cb);
			div.appendChild(span);
			cl.appendChild(div);
			bind(cb,"click",selColumn)
		}
		return false;
	}
	//定制需要显示的列
	var selColumn = function(){
		var c = me.columns[this.value];
		var b = c.checked;
		c.checked = this.checked;
		var cbc = 0;
		var wt = 0;  	//表格宽度
		for(var i=0; i<me.columns.length; i++){
			var column = me.columns[i];
			if (column.checked){
				if (column.type == "checkbox" || column.type == "radio"){
					cbc ++;
				}else{
					wt += column.original;
				}
			}
		}
		if (wt == 0) {		//表格宽度如果为0，表示没有选择的列。列设置不能为空，重置状态并返回
			c.checked = b;
			this.checked = b;
			return;
		};
		if (forceFit != true){
			var bw = tableWidth - cbc * QueryTable.CR_SIZE;
			var j = 0;
			for (var i=0;i<me.columns.length;i++){
				var column = me.columns[i];
				if (column.checked){
					if (column.type == "checkbox" || column.type == "radio"){
						
					}else{
						column.width = Math.round(bw * column.original/wt);
						j += column.width;
					}
				}
			}
			for (var i=me.columns.length-1;i>-1;i--){
				var column = me.columns[i];
				if (column.checked){
					column.width += bw - j;
					break;
				}
			}
		}else{
			tableWidth = tableWidth + c.width*(this.checked?1:-1) + 1
		}
		createHeadTable(me.tableId+"_heads");
		createDataTable()
	} 
	var hidConfLayer = function(){
		if (confLayer) confLayer.style.display = "none";
	}
	var showConfLayer = function(){
		if (confLayer) confLayer.style.display = "";
	}
	var hideExportLayer = function(){
		if (exportLayer) exportLayer.style.display = "none";
	}
	var showExportLayer = function(){
		if (exportLayer) exportLayer.style.display = "";
	}
	
	//托拽方法
	var mouseDown = function(){
		this.moveStart = true;
		this.startX = event.clientX;
		this.cTd = this.parentNode;
		if (!forceFit)
			this.cTdW = me.columns[this.cTd.column].width;
		this.pTd = getPrevious(this.cTd);
		this.pTdW = me.columns[this.pTd.column].width;
		var pc = me.columns[this.pTd.column]
		this.setCapture();
		var p = absp(this.cTd)
		separLayer.style.display = "";
		separLayer.style.left = p.left;
		separLayer.start = p.left;
		separLayer.style.top = p.top;
		separLayer.style.height = ($(dtId)?$(dtId).offsetHeight:0)+$(htId).offsetHeight;
	}
	var mouseUp = function(){
		this.releaseCapture();
		
		var w = event.clientX - this.startX;
		if (forceFit){
			var pw = this.pTd.offsetWidth + w;
			if (pw>12){
				var pc = this.pTd.column;
				var pd = $(bOdId(pc));
				pd.style.width = (pd.offsetWidth + w) + "px";				//何故－1呢
				me.columns[pc].width = pw;
				var len = me.isFull?me.maxResult:gds().objects.length;
				for (var i=-1; i<len; i++){
					var pTd = $(bTdId(i,pc));
					pTd.style.width = pw + "px";
				}
				tableWidth = tableWidth + w;
				$(htId).style.width = tableWidth + "px";
				$(dtId).style.width = tableWidth + "px";
			} 
		}else{
			var pw = this.pTdW + w
			var cw = this.cTdW - w;
			if (pw>12&&cw>12){
				var pc = this.pTd.column;
				var cc = this.cTd.column;
				var pd = $(bOdId(pc));
				var cd = $(bOdId(cc));
				pd.style.width = (pd.offsetWidth + w) + "px";
				cd.style.width = (cd.offsetWidth - w) + "px";
				me.columns[pc].width = pw;
				me.columns[cc].width = cw;
				var len = me.isFull?me.maxResult:gds().objects.length;
				for (var i=-1; i<len; i++){
					var pTd = $(bTdId(i,pc));
					var cTd = $(bTdId(i,cc));
					pTd.style.width = pw + "px";
					cTd.style.width = cw + "px";
				}
			}
		}
		separLayer.style.display = "none";
		
		this.moveStart = false;
		this.startX = 0;
	}
	var mouseMove = function(){
		if (this.moveStart){
			var w = event.clientX - this.startX;
			separLayer.style.left = separLayer.start + w
		}
	}
	var getPrevious = function(e){
		var node = e.previousSibling;
		while (node){
			if (node.style.display == "none"){
				node = node.previousSibling;
			}else
				return node;
		}
		return null
	}
	
	var trOver = function(){
		this.style.backgroundColor = QueryTable.OVER_COLOR;
	}
	
	var trOut = function(){
		var color = "";
		var cb = $(bCbId(this.floor));
		if (cb&&cb.checked) color = QueryTable.CHECKED_COLOR;
		this.style.backgroundColor = color;
	}
	
	var trClick = function(){
		var node = window.event.srcElement?window.event.srcElement:event.target;
		if (node.tagName=="INPUT"||node.tagName=="A")
			return;
		var color = "";	
		var cb = $(bCbId(this.floor));
		if (cb){
			if (cb.type == "radio"){
				if (checkRadio){
					var tr = $(bTrId(checkRadio.floor));
					if (tr)
						tr.style.backgroundColor = "";
				}
				checkRadio = $(bCbId(this.floor));
			}
			if (!cb.checked)
				color = QueryTable.CHECKED_COLOR;
			cb.checked = !cb.checked;
		}		
		this.style.backgroundColor = color;
	}
	
	var cbAll = function(){
		for (var i=0;i<gds().objects.length;i++){
			$(bCbId(i)).checked = this.checked;
			$(bTrId(i)).style.backgroundColor = this.checked?QueryTable.CHECKED_COLOR:"";
		}
	}
	
	var cbOne = function(){
		var color = "";
		if (this.checked) color = QueryTable.CHECKED_COLOR;
		if (this.type == "radio") {
			if (checkRadio){
				var tr = $(bTrId(checkRadio.floor));
				if (tr)
					tr.style.backgroundColor = "";
			}
			checkRadio = this
		}
		var tr = $(bTrId(this.floor))
		tr.style.backgroundColor = color;
	}
	
	//页脚翻页
	var createPaginate = function(divId){
		var previewText = "<img width=18 height=15 src='../images/arrow_left.gif' onmouseover=this.src='../images/arrow_left_over.gif' onmouseout=this.src='../images/arrow_left.gif'>";
		var nextText =  "<img width=18 height=15 src='../images/arrow_right.gif' onmouseover=this.src='../images/arrow_right_over.gif' onmouseout=this.src='../images/arrow_right.gif'>";
		var previewTextOff = "<img width=18 height=15 src='../images/arrow_leftoff.gif'>";
		var nextTextOff =  "<img width=18 height=15 src='../images/arrow_rightoff.gif'>";
		var total = gds().total;
		var start = gds().start;
		var maxResult = gds().maxResult;
		var pageHTML="";
		var temp = total-(total%maxResult!=0?total%maxResult:maxResult);
		var pages=temp/maxResult+1;	
		pageHTML += "<table cellSpacing=0 cellPadding=4 width=100% border=0 class=page><tr>";
		pageHTML += "<td width=33%>每页<select id="+me.tableId+"pageLen name="+me.tableId+"pageLen>";
		for (var i = 1;i<=me.minRows;i++){
			pageHTML += "<option value="+(i*me.pageMultiple)+""+(i*me.pageMultiple==maxResult?" selected":"")+">"+(i*me.pageMultiple)+"</option>"				
		}
		pageHTML += "</select>条  共<span>"+total+"</span>条</td>";
				
		pageHTML += "<td width=33% align=center>"
		if (start>0)
			pageHTML += "<a id='"+me.tableId+"first' href=# title='首页'>首页</a>";
		else
			pageHTML += "首页";
		pageHTML += "&nbsp;";
		
		if (start>=maxResult)
			pageHTML += "<a id='"+me.tableId+"forward' href=# title='前"+maxResult+"条'>上一页</a>";
		else
			pageHTML += "上一页";
		pageHTML += "&nbsp;";
			
		if ((start+maxResult)<total){
			pageHTML+="<a id='"+me.tableId+"next' href=# title='后"+maxResult+"条'>下一页</a>";
		}else{
			pageHTML+= "下一页";;
		}
		pageHTML += "&nbsp;";
							
		if (start < (pages-1)*maxResult)
			pageHTML += "<a id='"+me.tableId+"last' href=# title='末页'>末页</a>";
		else
			pageHTML += "末页";
			
		pageHTML += "</td>";
		
		pageHTML += "<td width=33%  align=right>";
		var pageTotal = pages?pages:1
		var currentPage = Math.round(start/maxResult,0)+1;
		
		pageHTML += " 共<span>"+pageTotal+"</span>页&nbsp;&nbsp;";
		pageHTML += "第<input type='text' size='1' id="+me.tableId+"pageNo value="+(currentPage?currentPage:1)+">页<button id="+me.tableId+"go_button name="+me.tableId+"pageNo>GO</button>";
		
		/**
		pageHTML += "页码&nbsp;&nbsp;<select id="+me.tableId+"pageNo name="+me.tableId+"pageNo>";
		
		for (var i=1;i<=pageTotal;i++){
			pageHTML += "<option value="+i+" "+(currentPage==i?"selected":"")+">"+i+"</option>";
		}
		//"+(start>=((i-1)*maxResult)&&start<i*maxResult?" selected":"")
		pageHTML +="</select>";	
		**/
		pageHTML +="</td>";	
		pageHTML += "</tr></table>";
		$(divId).innerHTML = pageHTML;
		
		bind(me.tableId + "first","click",function(){me.onclickActions(0);return false;})
		bind(me.tableId + "forward","click",function(){me.onclickActions(start-maxResult);return false;})
		bind(me.tableId + "next","click",function(){me.onclickActions(start+maxResult);return false;})
		bind(me.tableId + "last","click",function(){me.onclickActions((pages-1)*maxResult);return false;})
		bind(me.tableId + "go_button","click",function(){
			var pNo = parseInt($(me.tableId + "pageNo").value);
			if (getTableLayer().style.display != "none") return;
			if (isNaN(pNo)) {showWarning("请输入页码");return;}
			if (pNo > pages){showWarning("输入页码数大于最大页数");return;}
			me.onclickActions((pNo-1)*maxResult);
		})
		bind(me.tableId + "pageLen","change",function(){
				me.maxResult = this.value;
				setCookie("pageLen",this.value)
				me.onclickActions(0);
			})
	}
	var initColumns = function(rColumns){
		var dcMap = null;
		for (var i=0;options.displayColumns&&i<options.displayColumns.length;i++){
			if (!dcMap) dcMap = {}
			dcMap[options.displayColumns[i]] = true;
		}	
		var c0,c1;
		var j = 0, t = 0, cbc = 0;
		var len = rColumns.length>columns.length?rColumns.length:columns.length;
		for(var i=0;i<len;i++){
			c0 = columns[i];
			c1 = rColumns[i];
			if (!c0) c0 = c1?c1:{}
			c0 = new QTColumn(c0);
			if (c1) c0.copy(c1);
			c0.id = tId ++;
			c0.col = i;
			if (dcMap && c0.title && !c0.hidden)
				c0.checked = dcMap[i]?true:false;
			else{
				if (!c0.title)				//TODO 用hidden判断最好,hidden没传递出来
					c0.checked = false;
				else
					c0.checked = true;
			}
			var w = parseInt(c0.width);
			if (isNaN(w)){ 
				j++;c0.width = 0;
			}else{
				t += w;
				c0.width = w;
			}
			if (c0.type=="checkbox" || c0.type=="radio") cbc++;
			colMap[c0.id] = c0;
			me.columns.push(c0);
		}
		
		initcols = true;
		buildColumnsWidth1(j,t,cbc);
		initView();
	}	
	//默认button
	//导出文件方法
	var exportList = function(type){
		var params = getParams()
		var path = contextRoot + "d/list/"+ me.queryName +"."+type
		var query = "";
		var prop
		for (prop in params) {
			if (typeof params[prop] != "function"&&params[prop]) {
				query += prop + "=" + encodeURIComponent(params[prop]) + "&";
			}
		}
		if (query!="")
			path += "?"+query
		//TODO put 'orderBy' , 'maxResult' and 'showFields'
		if (type=='pdf'||type=='xls'||type=='rtf'||type=='csv'){
			document.location.href=path
		}else if (type=='xml')
			window.open(path)
	}
	this.getBoxObjs = function(){
		var boxs = []
		for (var i=0;i<gds().objects.length;i++){
			boxs.push($(bCbId(i)));
		}
		return boxs;
	}
	this.getBoxValue = function(isArray){
		var ids = "";
		if (isArray)
			ids = [];
		var objs = gds().objects;
		for (var i=0;i<objs.length;i++){
			var obj = objs[i]
			var cb = $(bCbId(i));
			if (cb.checked){
				if (isArray){
					ids.push(obj);
				}else{
					if (ids) ids += ",";
					ids += cb.value;
				}
			}
		}
		return ids;
	}
	
	this.getRadioValue = function(all){
		var value = null;
		for (var i=0;i<gds().objects.length;i++){
			var cb = $(bCbId(i));
			if (cb.checked){
				if (all)
					value = gds().objects[i];
				else
					value = cb.value;
				break;
			}
		}
		return value;
	}
	this.getBts = function(){
		var bts = $(me.tableId + "_btns").getElementsByTagName("BUTTON")
		var array = []
		for (var i=0;i<bts.length;i++){
			var b = bts[i];
			array.push(b);
		}
		return array;
	}
	this.clearTable = function(){
		this.dataSrc = null;
		createDataTable();
		getTableLayer();	
	}
	this.showTable = function(){
		if (!initcols) {showData = true;return};
		if (loading) return;
		this.params = getParams();
		this.start = 0
		this.onclickActions(this.start);
	}
	this.getBtnById = function(id){
		return $(me.tableId + "_btn_" + id)
	}
	this.onclickActions = function(start){
		orderFlag = true;
		if (this.checkFunc){
			if (!this.checkFunc())
				return;
		}else if (this.searchFormId&&!checkForm(this.searchFormId)){
			return;
		}
		if (tableLayer) tableLayer.style.display = "none";
		if (this.queryName){
			loading = true;			
		
			DWR.call("ejbQueryService.selectData",this.queryName,this.params,this.orderby,start,this.maxResult,null,function(pageObj){
				me.dataSrc = pageObj;
				createDataTable();			
				if (me.callback)
					me.callback(gds().objects)
			},{finalFunc:function(){loading = false;},blockScreen:true})//loading:this.tableId
		}else if (this.service){
			loading = true;			
		
			DWR.call(this.service,this.params,this.orderby,start,this.maxResult,null,function(pageObj){
				me.dataSrc = pageObj;
				createDataTable();			
				if (me.callback)
					me.callback(gds().objects)
			},{finalFunc:function(){loading = false;},blockScreen:true})//loading:this.tableId
			
		}else
			createDataTable();		//for test datasource
	}
	var initDiv = function(){
		var div = $(me.tableId)
		var html = '';
			html += '<div class="listBorder">';
			html += '<div class="listBar"'+(options.displayBar == "none"||options.displayBar == false?' style="display:none;"':'')+'>';
	        html += '<div class="listTitle"><span id="'+me.tableId+'_title"></span>&nbsp;<a href=# id="'+me.tableId+'_conf">定制</a></div>';
	        html += '<div class="listBtn" id="'+me.tableId+'_btns">'
	        html += '</div></div>';
			html += "<div class=list>";
			html += '<div id="'+me.tableId+'_heads" class="qthead"></div>'
			html += '<div id="'+me.tableId+'_datas" class="qtdata"></div>'
			html += '<div id="'+me.tableId+'_page"></div><div id="'+me.tableId+'_separ" style="display:none;" class="separlayer"></div></div>';
		div.innerHTML = html;
		if (columnsConf === false)
			$(me.tableId+"_conf").style.display = "none";
		else{
			bind(me.tableId+"_conf","click",confFunc);
			bind(me.tableId+"_conf","mouseout",hidConfLayer);
		}
		createButtons(me.tableId+"_btns");
	}();
	this.initTable = function(){
		//初始化columns，从后台取得columns设置，与前台设置columns设置组合
		if (me.queryName)
			DWR.call("ejbQueryService.getTableMeta",me.queryName, getParams(),function(meta){
				if (!me.title)
					me.title = meta.title;
				initColumns(meta.columns);	
			});
		else
			initColumns([]);
	}
	this.initTable();
}

QueryTable.CR_SIZE = 24;									//checkbox radio列宽度
QueryTable.TABLE_LAYER_CLASS = "tablelayer";				//未执行查询前的空表格样式
QueryTable.HEADER_LEFT_CLASS = "header_leftdiv";			//拖动列宽度的div样式
QueryTable.NORMAL_LEFT_CLASS = "normal_leftdiv";			//拖动列宽度的div样式
QueryTable.LAST_LEFT_CLASS = "last_leftdiv";				//拖动列宽度的div样式

QueryTable.DATA_TD_DIV = "datatddiv";
QueryTable.ORDERBY_DESC = "desc";							//倒排样式
QueryTable.ORDERBY_ASC = "asc";								//顺序样式
QueryTable.DRAG_LAYER_CLASS = "draglayer";					//托拽列层样式
QueryTable.DARG_LAYER_CONTENT_YES = "d_l_content_yes";		//可以放置的位置
QueryTable.DARG_LAYER_CONTENT_NO = "d_l_content_no";		//不可以放置的位置
QueryTable.DARG_LAYER_CONTENT_TITLE = "d_l_c_title";		//托拽列内容
QueryTable.FLAG_LAYER_CLASS = "flaglayer";					//列位置标志层
QueryTable.COL_MOVE_TOP = "col_move_top";					
QueryTable.COL_MOVE_MIDDLE = "col_move_middle";					
QueryTable.COL_MOVE_BOTTOM = "col_move_bottom";					
QueryTable.CONF_LAYER_CLASS = "conflayer";					//定制显示列样式
QueryTable.EXPORT_DIV_OUT = "exportdiv_out";				//导出div样式
QueryTable.EXPORT_DIV_OVER = "exportdiv_over";				//导出div样式


QueryTable.OVER_COLOR = "#CBE2FF";
QueryTable.CHECKED_COLOR = "#ffffcc";
